﻿using System;
using System.Collections.Generic;
using System.Linq;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using RayD.Core;
using RayD.App.Magicodes;
using RayD.WebApi.Models.Admin;
using RayD.Repository;
using RayD.Repository.Core;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 省市区资源
    /// </summary>
    public class SsqController : BaseAdminController
    {

        #region 获取数据
        /// <summary>
        /// 获取树型列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<SsqTreeModel>> GetTreeList()
        {
            var value = CacheHelper.Cache.GetCache<List<SsqTreeModel>>($"{AppSettings.ProjectName.EName}_SsqTreeList");
            if (value.IsEmpty())
            {
                //获取树型
                var list = this._GetTreeList();
                //存入缓存
                CacheHelper.Cache.SetCache($"{AppSettings.ProjectName.EName}_SsqTreeList", list, TimeSpan.FromMinutes(Define.EXPIRETIME), ExpireType.Relative);

                return Success(list);
            }
            else
            {
                return Success(value);
            }
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 从配置文件读取数据源
        /// </summary>
        /// <returns></returns>
        private ProvinceList[] _GetSource()
        {
            var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
                                                              .AddJsonFile("SSQ.json", optional: true, reloadOnChange: true)
                                                              .Build();
            return configuration.Get<SsqModel>().ProvinceList;
        }
        /// <summary>
        /// 获取树型列表数据
        /// </summary>
        /// <returns></returns>
        private List<SsqTreeModel> _GetTreeList()
        {
            //获取数据源
            var provinces = _GetSource();
            //返回树型
            List<SsqTreeModel> treeList = new List<SsqTreeModel>();
            //省
            foreach (var province in provinces)
            {
                SsqTreeModel node1 = new SsqTreeModel();
                node1.id = province.code;
                node1.value = province.code;
                node1.label = province.name;
                node1.parentId = "0";
                node1.data = province.name;
                treeList.Add(node1);
                //市
                if (!province.cityList.IsEmpty())
                    foreach (var city in province.cityList)
                    {
                        SsqTreeModel node2 = new SsqTreeModel();
                        node2.id = city.code;
                        node2.value = city.code;
                        node2.label = city.name;
                        node2.parentId = province.code;
                        node2.data = city.name;
                        treeList.Add(node2);
                        //区
                        if (!city.areaList.IsEmpty())
                            foreach (var area in city.areaList)
                            {
                                SsqTreeModel node3 = new SsqTreeModel();
                                node3.id = area.code;
                                node3.value = area.code;
                                node3.label = area.name;
                                node3.parentId = city.code;
                                node3.data = area.name;
                                treeList.Add(node3);
                            }
                    }
            }
            return treeList.ToTree().ToList();
        }
        #endregion

    }
}
